package com.athena.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.athena.common.core.domain.model.LoginUser;
import com.athena.common.helper.LoginHelper;
import com.athena.common.utils.DateUtils;
import com.athena.common.utils.StringUtils;
import com.athena.common.core.page.TableDataInfo;
import com.athena.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.athena.crm.domain.CrmClueFollrecords;
import com.athena.crm.domain.CrmCustomer;
import com.athena.crm.domain.bo.CrmClueFollrecordsBo;
import com.athena.crm.domain.bo.CrmCustomerFollrecordsAddBo;
import com.athena.crm.mapper.CrmCustomerMapper;
import com.athena.crm.service.ICrmCustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.athena.crm.domain.bo.CrmCustomerFollrecordsBo;
import com.athena.crm.domain.vo.CrmCustomerFollrecordsVo;
import com.athena.crm.domain.CrmCustomerFollrecords;
import com.athena.crm.mapper.CrmCustomerFollrecordsMapper;
import com.athena.crm.service.ICrmCustomerFollrecordsService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户跟进记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-12
 */
@RequiredArgsConstructor
@Service
public class CrmCustomerFollrecordsServiceImpl implements ICrmCustomerFollrecordsService {

    private final CrmCustomerFollrecordsMapper baseMapper;

    private final CrmCustomerMapper crmCustomerMapper;

    /**
     * 查询客户跟进记录
     */
    @Override
    public CrmCustomerFollrecordsVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户跟进记录列表
     */
    @Override
    public TableDataInfo<CrmCustomerFollrecordsVo> queryPageList(CrmCustomerFollrecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CrmCustomerFollrecords> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(CrmCustomerFollrecords::getUpdateTime);
        Page<CrmCustomerFollrecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户跟进记录列表
     */
    @Override
    public List<CrmCustomerFollrecordsVo> queryList(CrmCustomerFollrecordsBo bo) {
        LambdaQueryWrapper<CrmCustomerFollrecords> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(CrmCustomerFollrecords::getUpdateTime);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CrmCustomerFollrecords> buildQueryWrapper(CrmCustomerFollrecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CrmCustomerFollrecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCustomerId()), CrmCustomerFollrecords::getCustomerId, bo.getCustomerId());
        lqw.eq(StringUtils.isNotBlank(bo.getFlowType()), CrmCustomerFollrecords::getFlowType, bo.getFlowType());
        lqw.eq(StringUtils.isNotBlank(bo.getFollowUser()), CrmCustomerFollrecords::getFollowUser, bo.getFollowUser());
        lqw.eq(StringUtils.isNotBlank(bo.getFollowContent()), CrmCustomerFollrecords::getFollowContent, bo.getFollowContent());
        lqw.eq(bo.getIsDeleted() != null, CrmCustomerFollrecords::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增客户跟进记录
     */
    @Override
    public Boolean insertByBo(CrmCustomerFollrecordsBo bo) {
        CrmCustomerFollrecords add = BeanUtil.toBean(bo, CrmCustomerFollrecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId() + "");
        }
        return flag;
    }

    /**
     * 修改客户跟进记录
     */
    @Override
    public Boolean updateByBo(CrmCustomerFollrecordsBo bo) {
        CrmCustomerFollrecords update = BeanUtil.toBean(bo, CrmCustomerFollrecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CrmCustomerFollrecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户跟进记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean insertByBbo(CrmCustomerFollrecordsAddBo bbo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        CrmCustomerFollrecordsBo bo = new CrmCustomerFollrecordsBo();
        bo.setCustomerId(bbo.getCustomerId());
        bo.setFollowContent(bbo.getFollowContent());
        bo.setFlowType(bbo.getFlowType());
        CrmCustomerFollrecords add = BeanUtil.toBean(bo, CrmCustomerFollrecords.class);
        validEntityBeforeSave(add);
        add.setFollowUser(loginUser.getUsername());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId() + "");
            Date nextFollowTime = DateUtils.parseDate(bbo.getNextFollowTime());
            CrmCustomer crmCustomer = crmCustomerMapper.selectById(bbo.getCustomerId());
            if (crmCustomer != null) {
                crmCustomer.setFollowStatus(bbo.getFollowStatus());
                crmCustomer.setLatestFollowRecords(bbo.getFollowContent());
                crmCustomer.setRealityFollowRecords(new Date());
                crmCustomer.setNextFollowTime(nextFollowTime);
            }
            crmCustomerMapper.updateById(crmCustomer);
        }
        return flag;
    }
}
